<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/19
 * Time: 9:30
 */
namespace app\driver\controller;
use think\Db;
class Eye extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //热门搜索
    public function hot(){
       $res =  Db::table('up_hot')->where('hot_status',1)->select();
       return json(['status' => 200, 'msg' => $res]);
    }
    public function index(){
        extract(input());
        $type = input('param.type');//1关注  2全部
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        if($type==1){
            $eye_f_user_id = Db::table('up_eye')->where('eye_user_id',$user_id)->column('eye_f_user_id');
            $where['a.order_jms_user'] = array('IN',$eye_f_user_id);
        }
        $where['a.order_type'] = array('not in',array(3,4));
        $where['a.order_status'] = 2;
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
            ->limit($limit,$num)
            ->where($where)
            ->order('a.order_fb_time desc')
            ->field('a.order_intro,ucz.zxlx_name,a.order_goods_m,a.order_fb_time,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_phone,uuu.user_username,uuu.user_image')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //搜索
    public function keyword(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type');
        $num = 10;
        $limit = ($page-1)*$num;
        $keyword = input('param.keyword');
        $res = array();
        if($keyword==''){
            return json(['status'=>200,'msg'=>$res]);
        }else{
            $map['district'] = array('like',"%$keyword%");
            $district_id= Db::table('up_area')->where($map)->column('district_id');
            if(count($district_id)>0){
                if($type==1){
                    $where['a.order_go_sheng|a.order_go_shi'] = array('IN',$district_id);
                }elseif ($type==2){
                    $where['a.order_to_sheng|a.order_to_shi'] = array('IN',$district_id);
                }
            }else{
                return json(['status'=>200,'msg'=>$res]);
            }
        }
        $where['a.order_type'] = array('not in',array(3,4));
        $where['a.order_status'] = 2;
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->join('up_u_user uuu','uuu.user_id = a.order_qy_user')
            ->limit($limit,$num)
            ->where($where)
            ->order('a.order_fb_time desc')
            ->field('a.order_intro,a.order_true,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //订单详情
    public function orderDetail(){
        extract(input());
        $id = input('param.order_id');
        $where['a.order_id'] = $id;
        $info = Db::table('up_order')->alias('a')->where($where)->find();
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
            ->join('up_car_zxlx ucz' ,'ucz.zxlx_id = a.zxlx_id','left')
            ->where($where)
            ->field('a.*,ua.district as go_sheng,ua1.district as go_shi,
                        ua2.district as to_sheng,ua3.district as to_shi,
                        uuu.user_phone,uuu.user_real_name,uuu.user_username,uuu.user_image
                        ,ucz.zxlx_name,uch.hwlx_name')
            ->find();

        $map['eye_user_id'] = $user_id;
        $map['eye_f_user_id'] = $info['order_jms_user'];
        $list = Db::table('up_eye')->where($map)->find();
        if($list['eye_id']!=''){
            $res['eye'] = 1;
        }else{
            $res['eye'] = 2;
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //关注取消关注
    public function eye(){
        extract(input());
        $type = input('param.type');
        $order_jms_user = input('param.order_jms_user');
        if($type==1){//关注
            $data['eye_user_id'] = $user_id;
            $data['eye_f_user_id'] = $order_jms_user;
            $data['eye_time'] = date('Y-m-d H:i:s');
            $res = Db::table('up_eye')->insertGetId($data);
        }elseif ($type==2){
            $where['eye_user_id'] = $user_id;
            $where['eye_f_user_id'] = $order_jms_user;
            $res = Db::table('up_eye')->where($where)->delete();
        }
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败']);
        }
    }

}